package com.tourex.ui;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import android.content.Context;
import android.content.Intent;
import android.graphics.Paint;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.view.animation.LinearInterpolator;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.android.volley.Response;
import com.google.gson.reflect.TypeToken;
import com.tourex.App;
import com.tourex.BaseActivity;
import com.tourex.C;
import com.tourex.C.bean.MyLocationInfo;
import com.tourex.C.bean.TourProjectInfo;
import com.tourex.R;
import com.tourex.thirdparty.volley.VolleyUtils;
import com.tourex.utils.AnimUtils;
import com.tourex.utils.BaseUtils;

public class GeneralListActivity extends BaseActivity {

	private ListView listView;
	private TourProjectInfoAdapter tourProjectInfoAdapter;
	private List<TourProjectInfo> tourProjectInfoList = new ArrayList<TourProjectInfo>();
	private TextView txMenuHot;

	private static final String E_TYPE = "e_type";
	private static final String E_CZID = "e_czid"; // 采摘id
	private String type;
	private String czid;
	String[] sortTypes;

	private View rightMenu;
	private View layDivSortType;
	private GridView gridView;
	private SortTypeAdapter sortTypeAdapter = new SortTypeAdapter();
	private List<String> sortTypeList = new ArrayList<String>();

	public static void actionStart(Context context, String type, String czid) {
		Intent intent = new Intent(context, GeneralListActivity.class);
		intent.putExtra(E_TYPE, type);
		intent.putExtra(E_CZID, czid);
		context.startActivity(intent);
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		setContentView(R.layout.activity_general_list);
		super.onCreate(savedInstanceState);
		type = getIntent().getStringExtra(E_TYPE);
		czid = getIntent().getStringExtra(E_CZID);
		sortTypes = getResources().getStringArray(R.array.general_sort_types);

		titleLayout.addLeftDrawableMenu(this, R.drawable.ico_back, 25, 25, onBackClickListener);
		rightMenu = titleLayout.addRightMenu(this, initMenuHot(), onMenuHotClickListener);
		titleLayout.setTitle(getTitleByType());

		listView = (ListView) findViewById(R.id.listview);
		tourProjectInfoAdapter = new TourProjectInfoAdapter(this, 0, tourProjectInfoList);
		listView.setAdapter(tourProjectInfoAdapter);
		listView.setOnItemClickListener(onItemClickListener1);

		requestApi03(App.i().mli, type, C.convention.sort_hot);

		layDivSortType = findViewById(R.id.lay_div_sort_type);
		layDivSortType.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				hiddenSortTypeView();
			}
		});
		gridView = (GridView) findViewById(R.id.gridview);
		gridView.setAdapter(sortTypeAdapter);
		gridView.setOnItemClickListener(onItemClickListener2);
	}

	private int getTitleByType() {
		int title = R.string.app_name;
		if (type.equals(C.convention.type_cz)) {
			title = R.string.pick;
		} else if (type.equals(C.convention.type_ms)) {
			title = R.string.cate_1;
		} else if (type.equals(C.convention.type_tg)) {
			title = R.string.shopping_1;
		} else if (type.equals(C.convention.type_njl)) {
			title = R.string.farm_1;
		} else if (type.equals(C.convention.type_jd)) {
			title = R.string.tourist_attraction;
		} else if (type.equals(C.convention.type_qz)) {
			title = R.string.baby_1;
		} else if (type.equals(C.convention.type_xl)) {
			title = R.string.road_1;
		}
		return title;
	}

	private View initMenuHot() {
		LinearLayout view = (LinearLayout) inflater.inflate(R.layout.part_view_title_menu_hot, null);
		txMenuHot = (TextView) view.findViewById(R.id.tx_menu_hot);
		return view;
	}

	private final long sortTypeMenuAnimTime = 100l;

	private void hiddenSortTypeView() {
		endAnimation(
				gridView,
				AnimUtils
						.init()
						.addScale(1f, 0.3f, 1f, 0f, Animation.RELATIVE_TO_SELF, 1f, Animation.RELATIVE_TO_SELF, 0f, 0l,
								sortTypeMenuAnimTime, new LinearInterpolator()).create());
		gridView.postDelayed(new Runnable() {
			@Override
			public void run() {
				layDivSortType.setVisibility(View.GONE);
				rightMenu.setEnabled(true);
			}
		}, sortTypeMenuAnimTime);
	}

	OnClickListener onMenuHotClickListener = new OnClickListener() {
		@Override
		public void onClick(View view) {
			rightMenu.setEnabled(false);
			startAnimation(
					gridView,
					AnimUtils
							.init()
							.addScale(0.3f, 1f, 0f, 1f, Animation.RELATIVE_TO_SELF, 1f, Animation.RELATIVE_TO_SELF, 0f, 0l,
									sortTypeMenuAnimTime, new LinearInterpolator()).create());
			resetSelectSortTypeView();
		}

		private void resetSelectSortTypeView() {
			sortTypeList.clear();
			sortTypeList.add(sortTypes[0]);
			if (type.equals(C.convention.type_tg) || type.equals(C.convention.type_jd)) {
				sortTypeList.add(sortTypes[1]);
			}
			if (type.equals(C.convention.type_jd) || type.equals(C.convention.type_njl)) {
				sortTypeList.add(sortTypes[2]);
			}
			sortTypeAdapter.notifyDataSetChanged();
			layDivSortType.setVisibility(View.VISIBLE);
		}
	};

	public void requestApi03(MyLocationInfo mli, String type, String sort) {
		// 表示当前城市ID，采摘的时候为采摘模块ID
		String cityCode = mli.cityCode;
		if (type.equals(C.convention.type_cz) && !TextUtils.isEmpty(czid)) {
			cityCode = czid;
		}

		Map<String, String> params = BaseUtils.initMap("wd", mli.lat, "jd", mli.lng, "type", type, "p", sort, "city", cityCode);
		VolleyUtils.postString(C.api.api03, params, new Response.Listener<String>() {
			@Override
			public void onResponse(String response) {
				List<TourProjectInfo> data = gson.fromJson(response, new TypeToken<List<TourProjectInfo>>() {
				}.getType());
				tourProjectInfoList.clear();
				tourProjectInfoList.addAll(data);
				tourProjectInfoAdapter.notifyDataSetChanged();
				if (tourProjectInfoList.size() < 1) {
					showToast(R.string.empty_data);
				}
			}
		});
	}

	OnItemClickListener onItemClickListener1 = new OnItemClickListener() {
		@Override
		public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
			TourProjectInfo tpi = tourProjectInfoList.get(position);
			if (TextUtils.isEmpty(tpi.url)) {
				showToast(R.string.null_url);
			} else {
				WapActivity.actionStart(GeneralListActivity.this, tpi.url, type, tpi.phone, tpi.jd, tpi.wd);
			}
		}
	};

	class TourProjectInfoAdapter extends ArrayAdapter<TourProjectInfo> {

		public TourProjectInfoAdapter(Context context, int resource, List<TourProjectInfo> objects) {
			super(context, resource, objects);
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			final TourProjectInfo tpi = getItem(position);
			View view;
			ViewHolder viewHolder;
			if (convertView == null) {
				view = LayoutInflater.from(getContext()).inflate(R.layout.list_content_general_info, null);
				viewHolder = new ViewHolder();

				viewHolder.layDistance = view.findViewById(R.id.layout_distance);
				viewHolder.txDistance = (TextView) view.findViewById(R.id.tx_distance);
				viewHolder.txTitle = (TextView) view.findViewById(R.id.tx_title);
				viewHolder.txRemark = (TextView) view.findViewById(R.id.tx_remark);
				viewHolder.txPrice = (TextView) view.findViewById(R.id.tx_price);
				viewHolder.txPriceOriginal = (TextView) view.findViewById(R.id.tx_price_original);
				viewHolder.imgGeneralInfo = (ImageView) view.findViewById(R.id.img_general_info);
				view.setTag(viewHolder);
			} else {
				view = convertView;
				viewHolder = (ViewHolder) view.getTag();
			}

			viewHolder.txTitle.setText(tpi.title);
			if (tpi.jl != 0.0f) {
				viewHolder.layDistance.setVisibility(View.VISIBLE);
				// viewHolder.txDistance.setText(String.format(getResources().getString(R.string.distance_1),
				// String.format("%.1f", tpi.jl / 1000)));
				viewHolder.txDistance.setText(String.format(getResources().getString(R.string.distance_1), String.valueOf(tpi.jl)));
			} else {
				viewHolder.layDistance.setVisibility(View.GONE);
			}
			if (tpi.price != 0.0f) {
				viewHolder.txPrice.setVisibility(View.VISIBLE);
				viewHolder.txPriceOriginal.setVisibility(View.VISIBLE);
				viewHolder.txPrice.setText(String.format(getResources().getString(R.string.price_1), String.format("%.1f", tpi.price)));
				viewHolder.txPriceOriginal.setText(String.format(getResources().getString(R.string.price_1),
						String.format("%.1f", tpi.price)));
				viewHolder.txPriceOriginal.getPaint().setFlags(Paint.STRIKE_THRU_TEXT_FLAG);
				viewHolder.txPriceOriginal.setVisibility(View.GONE);
			} else {
				viewHolder.txPrice.setVisibility(View.GONE);
				viewHolder.txPriceOriginal.setVisibility(View.GONE);
			}

			if (!TextUtils.isEmpty(tpi.remark)) {
				viewHolder.txRemark.setVisibility(View.VISIBLE);
				viewHolder.txRemark.setText(tpi.remark);
			} else {
				viewHolder.txRemark.setVisibility(View.GONE);
			}

			imageLoader.get(tpi.img, getDefaultImageListener(viewHolder.imgGeneralInfo));

			return view;
		}

		class ViewHolder {
			ImageView imgGeneralInfo;
			TextView txTitle;
			TextView txDistance;
			TextView txRemark;
			TextView txPrice;
			TextView txPriceOriginal;

			View layDistance;
		}
	}

	class SortTypeAdapter extends BaseAdapter {

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			String sortType = (String) getItem(position);
			View view;
			if (convertView == null) {
				view = inflater.inflate(R.layout.list_content_sort_type, null);
			} else {
				view = convertView;
			}
			TextView textView = (TextView) view.findViewById(R.id.textview);
			textView.setText(sortType);
			return view;
		}

		@Override
		public int getCount() {
			return sortTypeList.size();
		}

		@Override
		public Object getItem(int position) {
			return sortTypeList.get(position);
		}

		@Override
		public long getItemId(int position) {
			return position;
		}
	}

	OnItemClickListener onItemClickListener2 = new OnItemClickListener() {
		@Override
		public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
			String sortType = sortTypeList.get(position);
			String sort = "";
			if (sortType.equals(sortTypes[1])) {
				sort = C.convention.sort_jg;
				txMenuHot.setText(sortTypes[1]);
			} else if (sortType.equals(sortTypes[2])) {
				sort = C.convention.sort_jl;
				txMenuHot.setText(sortTypes[2]);
			} else { // default
				sort = C.convention.sort_hot;
				txMenuHot.setText(sortTypes[0]);
			}
			requestApi03(App.i().mli, type, sort);
			hiddenSortTypeView();
		}
	};

}
